﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OilRigSumoWrestlers.Controllables.Controllers;
using OilRigSumoWrestlers.Entities.MenuItems;
using RaptorEngine;
using RaptorEngine.Components.Drawable;
using RaptorEngine.Managers;

namespace OilRigSumoWrestlers.Scenes.Menu
{
    public class PauseMenu : MenuScene
    {
        public PauseMenu(LogicalPlayerIndex player)
            : base(false)
        {
            CreateScene();
            CreateController(player);
        }

        private void CreateController(LogicalPlayerIndex player)
        {
            Entity pauseController = new Entity();
            pauseController.AddComponent(new MenuControllable(pauseController, this, player));
            AddEntity(pauseController);
        }

        private void CreateScene()
        {
            //Entity e = new Entity();
            //e.AddComponent(new DrawableText(e, GameEngine.Instance.Load<SpriteFont>("Fonts/testFont"), "Detta fungerar."));
            //AddEntity(e);

            Entity bgImage = new Entity();
            bgImage.AddComponent(new DrawableSprite(bgImage, GameEngine.Instance.Load<Texture2D>("Images/black"), Color.Black, 0.6f));
            bgImage.Scale = GameEngine.Instance.Render.GraphicsDevice.Viewport.Width;
            AddEntity(bgImage);

            //e = new Entity();
            //e.Position = new Vector3(400, 100, 0);
            //e.AddComponent(new DrawableSprite(e, GameEngine.Instance.Load<Texture2D>("Images/Frankytauro")));
            //AddEntity(e);

            //e = new Entity();
            //e.Position = new Vector3(GameEngine.Instance.Render.GraphicsDevice.Viewport.Width / 2 + 20, GameEngine.Instance.Render.GraphicsDevice.Viewport.Height / 2 + 130, 0);
            //e.AddComponent(new DrawableText(e, GameEngine.Instance.Load<SpriteFont>("Fonts/testFont"), "KENTAURU MOODO!!!!"));
            //AddEntity(e);


            Vector2 worldCenter = new Vector2(
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.X + GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Width / 2,
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Y + GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Height / 2);

            //Create Text
            string pauseMenuText = "Pause Menu";

            Entity pauseMenu = new Entity(worldCenter + new Vector2(0, -60));
            pauseMenu.AddComponent(new DrawableText(pauseMenu,
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuFont"), pauseMenuText, Color.White, 1, true));

            Entity pauseMenuShadow = new Entity(worldCenter + new Vector2(0, -60) + Vector2.One);
            pauseMenuShadow.AddComponent(new DrawableText(pauseMenuShadow,
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuFont"), pauseMenuText, Color.Black, 1, true));

            //Add texts to the scene
            AddEntity(pauseMenuShadow);
            AddEntity(pauseMenu);


            // Continue text
            string continueText = "Continue";

            DrawableText optContinueSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 40)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), continueText, Color.White, 1, true);

            DrawableText optContinue = new DrawableText(new Entity(worldCenter + new Vector2(0, 40)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), continueText, Color.Gray, 1, true);


            //Exit to main menu text
            string exitToMainMenuText = "Exit to menu";

            DrawableText optExitToMainMenuSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitToMainMenuText, Color.White, 1, true);

            DrawableText optExitToMainMenu = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitToMainMenuText, Color.Gray, 1, true);

            //Exit Game text
            string exitGameText = "Exit Game";

            DrawableText optExitGameSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitGameText, Color.White, 1, true);

            DrawableText optExitGame = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitGameText, Color.Gray, 1, true);


            _menuItems.Add(new ContinueGameItem(optContinueSelected, optContinue));
            _menuItems.Add(new ExitToMenuItem(optExitToMainMenuSelected, optExitToMainMenu));
            _menuItems.Add(new ExitGameItem(optExitGameSelected, optExitGame));
            _menuItems[_selectedItem].Select();

            foreach (MenuItem item in _menuItems)
            {
                AddEntity(item);
            }
        }


        public override void GoBack()
        {
            GameEngine.Instance.Scene.RemoveCurrentScene();
            GameEngine.Instance.Scene.StartCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
            GameEngine.Instance.Audio.ResumeMusic();
            base.GoBack();
        }

        public override void Update()
        {
            
        }
    }
}
